package com.csht.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.csht.model.CshtCriminalRecords;
import com.csht.model.CshtUser;
import com.csht.page.PagerModel;
import com.csht.page.SystemContext;
import com.csht.service.CshtCriminalRecordsService;

/**
 * 违规记录
 * @author chenhuiyan
 *
 */
@Controller
@RequestMapping(value = "/toMy")
public class CriminalRecordsController {
	
	@Autowired
	private CshtCriminalRecordsService cshtCriminalRecordsService;
	
	/**
	 * 违规记录首页
	 * @param model
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/criminalRecords")
	public String criminalRecords(ModelMap model, HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser");
		Map<String, Integer> res = new HashMap<String, Integer>();
		res.put("userId", cshtUser.getUserId());
		res.put("offset", SystemContext.getOffset());
		res.put("messagePageSize", SystemContext.getMessagePageSize());
		PagerModel<CshtCriminalRecords> pagerModel = cshtCriminalRecordsService.getCriminalRecordsByUserId(res);
		model.addAttribute("pagerModel", pagerModel);
		return "/toMy/criminalRecords";
	}
	
	/**
	 * 查询非常严重违规次数
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/levelTimes")
	public Map<String, Integer> levelTimes(HttpServletRequest request) throws Exception{
		CshtUser cshtUser = (CshtUser) request.getSession().getAttribute("loginUser");
		Integer levelTimes = cshtCriminalRecordsService.getLevelTimesByUserId(cshtUser.getUserId());
		Map<String, Integer> res = new HashMap<String, Integer>();
		res.put("levelTimes", levelTimes);
		return res;
	}
	
	/**
	 * 根据违规记录id查询
	 * @param levelId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/getCriminalRecordsById")
	public Map<String, CshtCriminalRecords> getCriminalRecordsById(@RequestParam(name = "levelId") Integer levelId) throws Exception{
		Map<String, CshtCriminalRecords> res = new HashMap<String, CshtCriminalRecords>();
		CshtCriminalRecords cshtCriminalRecords = cshtCriminalRecordsService.getCriminalRecordsById(levelId);
		res.put("cshtCriminalRecords", cshtCriminalRecords);
		return res;
	}

}
